package com.woniuxy.microsysserver.controller;

import com.woniuxy.common.bean.ResultMsg;
import com.woniuxy.microsysserver.util.IpUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/rabbit")
@RestController
public class RabbitMQController {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 直接交换机
     * @param routeKey
     * @return
     */
    @RequestMapping(value = "/login",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg direct(Integer id, String routeKey){
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ipAddr = IpUtil.getIpAddr(req);
        /*将数据封装成Map对象*/
        Map<String,Object> data = new HashMap<>();
        data.put("id",id);
        data.put("ip",ipAddr);
        rabbitTemplate.convertAndSend("loginExchange",routeKey,data);
        return new ResultMsg(true);
    }
}
